package com.bizfx.component.views
{
	import flexlib.controls.TreeGrid;
	import flexlib.controls.treeGridClasses.TreeGridColumn;
	
	import com.bizfx.component.controls.ListItemSelectorColumn;
	import com.bizfx.component.controls.ListItemSelectorHeaderRenderer;
	import com.bizfx.utils.TreeUtils;
	import com.bizfx.utils.ObjectUtils;
	
	import mx.collections.IList;
	import mx.controls.CheckBox;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.core.ClassFactory;
	import mx.events.FlexEvent;
	import mx.formatters.DateFormatter;
	
	public class BaseTreeListView extends BaseListView implements IListView
	{
		private var _openLevel:int = 0;
		
		public function get openLevel():int {
			return this._openLevel;
		}
		
		public function set openLevel(value:int):void {
			this._openLevel = value;
		}
		
		private var dateFormatter:DateFormatter = new DateFormatter();
		
		private function get thisList():TreeGrid {
			if (this.list != null) {
				return this.list as TreeGrid;
			}
			return null;
		}

		public function BaseTreeListView() {
			this.paging = false;
		}
		
		override protected function createList():void {
			super.createList();
			if (this.thisList == null) {
				this.list = new TreeGrid();
				this.list.allowMultipleSelection = this.allowMultipleSelection;
				this.list.percentHeight = 100;
				this.list.percentWidth = 100;
			}
			
			this.thisList.columns = new Array();
			if (this.allowMultipleSelection) {
				var selectorColumn:ListItemSelectorColumn = new ListItemSelectorColumn();
				selectorColumn.sortable = false;
				selectorColumn.resizable = false;
				selectorColumn.draggable = false;
				selectorColumn.width = 28;
				selectorColumn.setStyle("paddingLeft", "6");
				selectorColumn.dataField = "__selector";
				selectorColumn.headerRenderer = new ClassFactory(ListItemSelectorHeaderRenderer);
				selectorColumn.itemRenderer = new ClassFactory(CheckBox);
				selectorColumn.addEventListener("headerClick", selectorHeaderHandler);
				thisList.columns = thisList.columns.concat(selectorColumn);
			}
			
			for (var i:int; i < this.columns.length; i++) {
				var column:ListViewColumn = this.columns.getItemAt(i) as ListViewColumn;
				if (column != null) {
					var item:DataGridColumn;
					if (i == 0) {
						item = new TreeGridColumn();	
					} else {
						item = new DataGridColumn();
					}
					
					item.headerText = column.text;
					item.dataField = column.dataField;
					if(column.labelFunction != null) {
						item.labelFunction = column.labelFunction;
					} else {
						item.labelFunction = formatColumnData;
					}
					if (column.width > 0) {
						item.width = column.width;
					}
					thisList.columns = thisList.columns.concat(item);
				}
			}
		}

		override public function set viewDataSource(value:Object):void {
			super.viewDataSource = value;
			TreeUtils.openItems(thisList, this.openLevel);
		}
		
		private function formatColumnData(item:Object,column:DataGridColumn):String {
			var data:Object = ObjectUtils.getPropertyValue(item, column.dataField);
			
			if (data != null) {
				if (data is Date) {
					if (data.valueOf() == new Date(0, 0, 0).valueOf()) {
						return "";
					} else {
						dateFormatter.formatString="YYYY-MM-DD";
						return dateFormatter.format(data);						
					}
				} else if (data is Boolean) {
					return data?"是":"否";
				} else if (data is Number) {
					//					dateFormatter.formatString="YYYY-MM-DD";
					//					return dateFormatter.format(item[column.dataField]);
					data.toString();
				}
				return data.toString();	
			} else {
				return null;
			}
		}
	}
}